package com.yxw.trade.util;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

import javax.swing.JPanel;

import com.yxw.trade.model.Kline;

public class KlinePanel extends JPanel {
	private static final long serialVersionUID = -2911523085339874791L;
	private List<Kline> klines;
	//k线宽度
	private int width = 6;
	//k线起始位置
	private int x = 10;
	//间距
	private int range = 1;
	public KlinePanel(int x, int y, int w, int h, List<Kline> klines){
		this.klines = klines;
		int panelWidth = this.width;
		if(klines != null){
			panelWidth += (this.width + this.range) * klines.size();
			if(panelWidth < w){
				panelWidth = w;
			}
		}
		this.setBounds(x, y, panelWidth, h);
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		if(klines != null){
			//y轴起始值
			int relative = 1800;
			for(int i = 0; i < klines.size(); i++){
				Kline k = klines.get(i);
				int open = (int) (k.getOpen() * 100);
				int close = (int) (k.getClose() * 100);
				int heightDot = (int) (k.getHigh() * 100);
				int lowDot = (int) (k.getLow() * 100);
				int y = open > close ? open : close;
				int height = Math.abs(open - close);
				// k线实体
				if(open >= close){
					g.setColor(Color.black);
					g.fillRect(x, relative - y, width, height);
				}else{
					g.setColor(Color.red);
					g.drawRect(x, relative - y, width, height);
				}
				
				//上影线
				if(heightDot > y){
					g.drawLine(x + this.width/2, relative - heightDot, x + this.width/2, relative - y);
				}
				//下影线
				if(lowDot < (y - height)){
					g.drawLine(x + this.width/2, relative - y + height, x + this.width/2, relative - lowDot);
				}
				
				//展示标记
				if(k.getType() > 0){
					String tag = null;
					if(k.getPen()==0){
						tag = "x";
					}else{
						tag = "" + k.getTag();
					}
						
					// 顶分型 底分型
					if(k.getType() == 1){
						g.drawString(tag, x, relative - heightDot - 10);
					}else{
						g.drawString(tag, x, relative - lowDot + 15);
					}
				}
				
				
				x += (this.width + this.range);
			}
		}
		g.dispose();
	}
}
